#include "test_tool.h"
#include "cocos2d.h"
#include "HelloWorldScene.h"
#include "AppDelegate.h"

USING_NS_CC;
using namespace std;


test_tool::test_tool(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);
}

test_tool::~test_tool()
{
	Uninit();
}

bool test_tool::Init()
{
	HWND hwnd = ui.centralWidget->winId();

	CCEGLView* pView = CCEGLView::sharedOpenGLView(hwnd);

	if(!pView)
	{
		return false;
	}

	m_pAppDelegate = new AppDelegate;

	m_timer.setParent( 0 );
	m_timer.setInterval( 60 ); // in msec 
	m_timer.setSingleShot( false ) ; 
	QObject::connect( &m_timer, SIGNAL( timeout() ), this, SLOT( OnTimeout() ) ) ; 

	m_timer.start();


	pView->setViewName("HelloCpp");
	pView->setFrameSize(2048, 1536);
	// The resolution of ipad3 is very large. In general, PC's resolution is smaller than it.
	// So we need to invoke 'setFrameZoomFactor'(only valid on desktop(win32, mac, linux)) to make the window smaller.
	pView->setFrameZoomFactor(0.4f);

	
	return m_pAppDelegate->applicationDidFinishLaunching();
}

void test_tool::OnTimeout()
{
	 CCApplication::sharedApplication()->runOnce();
}

void test_tool::Uninit()
{
	m_timer.stop();
	CCEGLView::sharedOpenGLView()->end();

	if(m_pAppDelegate)
	{
		delete m_pAppDelegate;
	}
}
